package com.xbjtech.ticketsystem.controller;

import com.xbjtech.ticketsystem.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: leejun.happy@gmail.com
 * @created: 2019年2月26日
 * 处理验证码请求
 */
@RestController
@Slf4j
@RequestMapping("/captcha")
public class CaptchaController {

    /**
     * 响应并显示验证码图片
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/create")
    public void createCode(HttpServletResponse response) throws IOException {
        response.setHeader("Expires", "-1");
        response.setContentType("application/x-jpg");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "-1");
        CaptchaUtil captchaUtil = CaptchaUtil.Instance();
        String code = captchaUtil.getString();
        SecurityUtils.getSubject().getSession().setAttribute(CaptchaUtil.SESSION_CODE_NAME, code);
        ImageIO.write(captchaUtil.getImage(), "JPG", response.getOutputStream());
    }

}